package Public

import (
	"errors"
	"time"
)

func StringTotime(timeLayout,toBeCharge string)(*time.Time,error)  {
	loc, err := time.LoadLocation("Local")                            //重要：获取时区
	if err != nil {
		return nil, errors.New("时区获取失败")
	}
	theTime, err := time.ParseInLocation(toBeCharge, timeLayout, loc) //使用模板在对应时区转化为time.time类型
	if err != nil {
		return nil, errors.New("时间转换失败")
	}
	return &theTime,nil
}

//获取上个月，开始，结束时间
func GetLastMonthStartEnd() (time.Time, time.Time) {
	now := time.Now()
	lastMonthFirstDay := now.AddDate(0, -1, -now.Day()+1)
	lastMonthStart := time.Date(lastMonthFirstDay.Year(), lastMonthFirstDay.Month(), lastMonthFirstDay.Day(), 0, 0, 0, 0, now.Location())
	lastMonthEndDay := lastMonthFirstDay.AddDate(0, 1, -1)
	lastMonthEnd := time.Date(lastMonthEndDay.Year(), lastMonthEndDay.Month(), lastMonthEndDay.Day(), 23, 59, 59, 0, now.Location())
	return lastMonthStart, lastMonthEnd
}